import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Gui.Global as G
import Backend as B
import Gui.Components

Item {
    id: root
    property bool manualSetSearchText: false
    TipSearchBox {
        id: _searchBox
        width: _searchInput.width
        x: _searchInput.x
        y: _searchInput.y + _searchInput.height
        B.SearchTipViewModel {
            id: searchTipViewModel

            onTipSearchWordsChanged: {
                if (searchTipViewModel.tipSearchWords.length > 0) {
                    _searchBox.open();
                } else {
                    _searchBox.close();
                }
            }
        }
        tipWords: searchTipViewModel.tipSearchWords
        onItemClicked: item => {
            manualSetSearchText = true;
            G.SignalBus.search(item);
            searchTipViewModel.clearTipSearchWords();
            _searchInput.text = item;
            manualSetSearchText = false;
        }
    }

    required property Window dragWindow
    WindowDragHandler {
        dragWindow: root.dragWindow
    }
    RowLayout {
        anchors.fill: parent
        TextField {
            id: _searchInput
            Layout.topMargin: 10
            Layout.leftMargin: 20
            Layout.rightMargin: 10
            Layout.preferredHeight: 35
            Layout.preferredWidth: 350
            rightPadding: 2 * height
            leftPadding: 10
            font.pixelSize: 14
            placeholderText: "总要搜点什么吧。。。"
            focus: true
            onAccepted: {
                searchBtn.click();
            }

            onTextChanged: {
                if (text.length > 0) {
                    if (!manualSetSearchText)
                        searchTipViewModel.getTipSearchWords(text);
                }
            }

            IconButton {
                id: clearBtn
                height: parent.height
                width: height
                anchors.right: searchBtn.left
                iconName: "fa.close"
                visible: opacity > 0
                onClicked: {
                    _searchInput.text = "";
                    _searchInput.forceActiveFocus();
                }
                opacity: _searchInput.text.length > 0 ? 1 : 0

                Behavior on opacity {
                    NumberAnimation {
                        duration: 200
                    }
                }
            }
            IconButton {
                id: searchBtn
                height: parent.height
                width: height
                anchors.right: parent.right
                iconName: "fa.search"
                raduises: [0, 4, 4, 0]
                onClicked: {
                    if (_searchInput.text.length > 0) {
                        G.SignalBus.search(_searchInput.text);
                        _searchBox.close();
                    }
                }
                cursorShape: Qt.PointingHandCursor
            }
            background: Rectangle {
                implicitWidth: 200
                implicitHeight: 40
                color: parent.activeFocus ? palette.midlight : palette.base
                radius: 4
            }
        }
        WindowOperationBar {
            dragWindow: root.dragWindow
            Layout.alignment: Qt.AlignRight | Qt.AlignTop
        }
    }
}
